'use strict';
/**
 * @ngdoc function
 * @name htmsMobileApp.controller:EntBaseInfoCtrl
 * @description
 * # EntBaseInfoCtrl 企业基本信息的控制器
 * Controller of the htmsMobileApp
 */
angular.module('htmsMobileApp').value("listxConfig", {
  template: "views/tpl/list-tpl.html",
  searchBarTemplate: "views/tpl/search-bar-tpl.html",
  itemsTemplate: "views/tpl/items-tpl.html",
  itemTemplate: "views/tpl/item-tpl.html"
})
  .controller('EntBaseInfoCtrl', function($scope,$log,$modal,$templateCache,CustomHttp,urlObj2Str,listxConfig) {
    //设置初始查询的url
    $scope.entsUrl = {
      apiUrl : "/api/entBaseInfo/list",
      page:1,
      pageSize:5,
      sortType:"qyid",
      search : [{
        key:"search_EQ_qyzt",value:"1"
      }]
    };

    //查询
    $scope.queryEnts = function(){
      var addQymcQuery = function(uo){
        if($scope.q.val) {
          uo.search.push({
            key: "search_LIKE_qymc",
            value: $scope.q.val
          })
        }
      }
      angular.element('#loadingAjaxEntBaseInfo').removeAttr("style");
      $scope.$parent.items = null;
      var uo = {
        apiUrl : "/api/entBaseInfo/list",
        page:1,
        pageSize:5,
        sortType:"qyid",
        search : []
      };
      if($scope.searchActionItemSelected){
        switch ($scope.searchActionItemSelected.item){
          case "名称":  addQymcQuery(uo);break;
          case "待核查": uo.search.push({
            key:"search_EQ_bdzt",value:"1"
          });addQymcQuery(uo);break;
          case "已评级": uo.search.push({
            key:"search_ISNOTNULL_qyjb",value:"1"
          });addQymcQuery(uo);break;
          default : console.log("查询条件都不匹配");
        }
        uo.search.push({
          key:"search_EQ_qyzt",value:"1"
        })
      }

      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.$parent.items = data.content;
        $scope.$parent.first = data.first;
        $scope.$parent.last = data.last;
        $scope.$parent.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntBaseInfo').css("display","none");
      })

    };
    $scope.resetMima = function(qyid){
      if(confirm("确认重置该企业的密码？")){
        CustomHttp.get("/api/entBaseInfo/resetPasswd/"+qyid).success(function(data){
            alert("重置成功");
        })
      }
    };
    $scope.zhuxiao = function(qyid){
      if(confirm("确认注销该企业？")){
        CustomHttp.get("/api/entBaseInfo/zhuxiao/"+qyid).success(function(data){
          alert("注销成功");
        })
      }
    };

    //下一页
    $scope.nextPage = function (){
      var uo;
      if($scope.uo)
      uo = $scope.uo;
      else uo = $scope.entsUrl;
      $scope.items = null;
      angular.element('#loadingAjaxEntBaseInfo').removeAttr("style");
      uo.page ++;
      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.items = data.content;
        $scope.first = data.first;
        $scope.last = data.last;
        $scope.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntBaseInfo').css("display","none");
      })
    };
    //上一页
    $scope.prePage = function (){
      var uo;
      if($scope.uo)
        uo = $scope.uo;
      else uo = $scope.entsUrl;
      $scope.items = null;
      angular.element('#loadingAjaxEntBaseInfo').removeAttr("style");
      uo.page --;
      var u = urlObj2Str.getUrlStr(uo);
      CustomHttp.get(u).success(function (data) {
        $scope.items = data.content;
        $scope.first = data.first;
        $scope.last = data.last;
        $scope.uo = uo;
        //去掉加载中的进度条
        angular.element('#loadingAjaxEntBaseInfo').css("display","none");
      })
    };
    //查询过滤方式
    $scope.searchActionItems = ['名称', '待核查','已评级'
      //,'自定义'
    ];
    //当前所选择的查询条件
    $scope.searchActionItemSelected = {item : $scope.searchActionItems[0]};
    //触发查询过滤方式
    $scope.doSearch = function(searchActionItem){
      //$scope.entsUrl.page = 1;
      //if($scope.$parent.uo){
      //  $scope.$parent.uo.page = 1;
      //  $scope.$parent.uo.search = [{
      //
      //  }];
      //}
      if(searchActionItem=='自定义'){
        var modalInstance = $modal.open({
          //给一个modal框的模板，根据名字获取
          templateUrl: 'myModalContent.html',
          //使用当前的控制器
          controller: 'EntBaseInfoCtrl',
          //给modal框写入一些值
          resolve: {
            searchActionItems: function () {
              return $scope.searchActionItems;
            }
          }
        });

      }
      $scope.searchActionItemSelected = {item : searchActionItem};
    };
    //自定义查询modal框
    $scope.ok = function(){
      console.log("这里是自定义查询操作");
    };
    //自定义查询modal框取消
    $scope.cancel = function(){
      console.log("这里是取消自定义查询的modal框");
    };

    //以下是对列表指令的操作
    $scope.searchBarTemplate = listxConfig.searchBarTemplate;
    $scope.itemsTemplate = listxConfig.itemsTemplate;
    $scope.itemTemplate = listxConfig.itemTemplate;
    $scope.itemTpl = false;
    $scope.q = {val: ""};
    $scope.isSelected = function (item) {
      if (item.selected) {
        return "active"
      }
    };
    $scope.selectItem = function (item) {
      var curItem, _i, _len, _ref;
      _ref = $scope.ngModel;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        curItem = _ref[_i];
        if (curItem.selected) {
          delete curItem.selected
        }
      }
      item.selected = true;
      return $scope.onSelect({item: item})
    };
    this.setItemTemplate = function (tpl, src) {
      $scope.itemTpl = true;
      if (src) {
        $scope.itemTemplate = src
      }
      return $templateCache.put("listxItemTpl", tpl)
    };
  }).controller('InfoDetailCtrl',function($scope,$routeParams,CustomHttp){
    console.log($routeParams.id);
    var qyid = $routeParams.id;
    CustomHttp.get('/api/entBaseInfo/'+qyid).
    success(function(data) {
      $scope.baseInfo = data;
        var sjqyid = data.sjdwqyid;
        if(sjqyid!=undefined&&sjqyid!=null&&sjqyid != 0){
          CustomHttp.get('/api/entBaseInfo/'+sjqyid)
            .success(function(data){
              $scope.sjdwqyInfo = data;
            })
            .error(function(data){

            });
        }
    }).
    error(function(data, status, headers, config) {

    });
  })

  .directive("listX", ["CustomHttp", "$templateCache", "listxConfig", function (CustomHttp, $templateCache, listxConfig) {
    return {
      restrict: "E",
      transclude: true,
      replace: true,
      require: "ngModel",
      scope: {
        title: "@",
        hideSearchBar: "@",
        itemHandlers: "&",
        //loadUrl ngModel 改成双向数据绑定
        loadUrl: "=",
        ngModel: "=",
        onSelect: "&",
        onLoad: "&"
      },
      templateUrl: function (tElement, tAttrs) {
        return listxConfig.template
      },
      //列表的所有操作都使用同一个控制器
      controller: "EntBaseInfoCtrl",
      link: function (scope, iElement, iAttrs, controller) {
        //初始化列表数据
        if (scope.loadUrl) {
          CustomHttp.get(scope.loadUrl).success(function (data) {
            scope.ngModel = data.content;
            scope.first = data.first;
            scope.last = data.last;
            //去掉加载中的进度条
            angular.element('#loadingAjaxEntBaseInfo').css("display","none");
            return scope.onLoad();
          })
        }
        $(".list-x-main div[ng-transclude]").remove();
        return $(".list-x-main").removeAttr("title")
      }
    }
  }]).directive("itemTemplate", function () {
    return {
      restrict: "E",
      replace: true,
      transclude: true,
      require: "^listX",
      scope: {itemTemplate: "@"},
      template: "<div ng-transclude></div>",
      link: function (scope, iElement, iAttrs, controller) {
        return controller.setItemTemplate(iElement.html(), scope.itemsTemplate)
      }
    }
  });
